package com.zhenhao.xsq.ui.activity.home;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.application.AppConfig;
import com.zhenhao.xsq.base.BaseFragment;
import com.zhenhao.xsq.entity.EvenbugMessage;
import com.zhenhao.xsq.ui.activity.camera.CameraListActivity;
import com.zhenhao.xsq.ui.activity.door.DoorManageActivity;
import com.zhenhao.xsq.ui.activity.door.InviteActivity;
import com.zhenhao.xsq.ui.activity.home.component.DaggerIndexComponent;
import com.zhenhao.xsq.ui.activity.home.contract.IndexContract;
import com.zhenhao.xsq.ui.activity.home.module.IndexModule;
import com.zhenhao.xsq.ui.activity.home.presenter.IndexPresenter;
import com.zhenhao.xsq.ui.activity.login.RegisterActivity;
import com.zhenhao.xsq.ui.activity.notice.NoticeActivity;
import com.zhenhao.xsq.utils.SpSaveUtils;
import com.zhenhao.xsq.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 欣物业
 *
 * @author xlei
 * @Package com.example.rxdemo.ui.activity.home
 * @Description: $description
 * @date 2019/01/08 10:12:45
 */

public class IndexFragment extends BaseFragment implements IndexContract.View {

    @Inject
    IndexPresenter mPresenter;

    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.iv_person)
    ImageView ivPerson;
    @BindView(R.id.tv_project)
    TextView tvProject;
    @BindView(R.id.tv_room_number)
    TextView tvRoomNumber;
    @BindView(R.id.tv_triangle_down)
    RelativeLayout tvTriangleDown;
    @BindView(R.id.banner_viewPager)
    ViewPager bannerViewPager;
    @BindView(R.id.banner_indicator)
    FixedIndicatorView bannerIndicator;
    @BindView(R.id.ll_znmj)
    LinearLayout llZnmj;
    @BindView(R.id.ll_fkyq)
    LinearLayout llFkyq;
    @BindView(R.id.ll_ggjk)
    LinearLayout llGgjk;
    @BindView(R.id.ll_tzgg)
    LinearLayout llTzgg;
    @BindView(R.id.tv_notice_1)
    TextView tvNotice1;
    @BindView(R.id.tv_notice_2)
    TextView tvNotice2;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;


    private View view;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_home_index, null);
            ButterKnife.bind(this, view);
            Bundle mBundle = getArguments();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(UIUtils
                    .getDisplayWidth(getActivity()),
                    UIUtils.getStatusBarHeight(getActivity()) * 3);

        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        initDataFromLocal();
    }

    private boolean isload;


    private void loadData() {
//        fwInfoJson = SpSaveUtils.getFwInfoJson();
//        yzInfoJson = SpSaveUtils.getYzInfoJson();
//
//        if (null != yzInfoJson && null != fwInfoJson) {
//            String xiangmuMc = yzInfoJson.getXiangmuMc();
//            if (TextUtils.isEmpty(xiangmuMc)) {
//                UserEntity userInfoJson = SpSaveUtils.getUserInfoJson();
//                UserEntity.DataBean.XiangmuVoBean xiangmuVo = userInfoJson.getData()
//                .getXiangmuVo();
//                if (!TextUtils.isEmpty(xiangmuVo.getLoupan())) {
//                    xiangmuMc = xiangmuVo.getLoupan();
//                }
//            }
//            tvAddress.setText("" + xiangmuMc + fwInfoJson.getQiqu() + fwInfoJson.getLoudong() +
//            "栋" +
//                    fwInfoJson.getDanyuan() + "单元" + fwInfoJson.getFanghao() + "号");
//
//            Map<String, String> map1 = new HashMap<>();
//            map1.put("loudong", "" + fwInfoJson.getLoudong());//楼栋
//            map1.put("danyuan", "" + fwInfoJson.getDanyuan());//单元
//            map1.put("qiqu", "" + fwInfoJson.getQiqu());//期区
//            map1.put("xiangmuBh", "" + yzInfoJson.getXiangmuBh());//项目编号
//            mPresenter.getBleList(map1);
//        }
//
//        Map<String, String> map = new HashMap<>();
//        mPresenter.getBannerTop(map);
    }


    @Override
    protected void setupFragmentComponent() {
        DaggerIndexComponent
                .builder()
                .appComponent(((AppConfig) getActivity().getApplication()).getApplicationComponent())
                .indexModule(new IndexModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(IndexContract.IndexContractPresenter presenter) {
        mPresenter = (IndexPresenter) presenter;
    }

    @Override
    protected void initDataFromLocal() {
        Logger.e("IndexFragment getUserVisibleHint() " + getUserVisibleHint() + " isUIVisible " + isUIVisible);
        // TODO: 2019/5/16 为什么这个isUIVisible永远都是false
//        if (!isViewCreated || !isUIVisible || isload) {
        if (!isViewCreated || isload) {
            return;
        }
        isload = true;
        loadData();
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        mPresenter.unsubscribe();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(EvenbugMessage message) {
        if (message.getType() == EvenbugMessage.MSG_HOME_REFRESH) {
            Log.e("刷新", "IndexFragment 响应");
            loadData();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
//        if (null != marqueeView) {
//            marqueeView.startFlipping();
//        }
    }

    @Override
    public void onStop() {
        super.onStop();
//        if (null != marqueeView) {
//            marqueeView.stopFlipping();
//        }

    }

    @OnClick({R.id.ll_znmj, R.id.ll_fkyq, R.id.ll_ggjk, R.id.ll_tzgg})
    public void onViewClicked(View view) {
//        SpSaveUtils.putToolbarColor(2);
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.ll_znmj:
                startActivity(DoorManageActivity.class, bundle);
                break;
            case R.id.ll_fkyq:
                startActivity(InviteActivity.class);
                break;
            case R.id.ll_ggjk:
                startActivity(CameraListActivity.class);
                break;
            case R.id.ll_tzgg:
                startActivity(NoticeActivity.class, bundle);
                break;
        }
    }
}